package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.entity.PageResult;
import com.gxa.jd.mapper.GoodsOrderMapper;
import com.gxa.jd.pojo.GoodsOrder;
import com.gxa.jd.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GoodsOrderServiceImpl implements GoodsOrderService {

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<GoodsOrder> findAll() {
        return goodsOrderMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<GoodsOrder> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<GoodsOrder> orders = (Page<GoodsOrder>) goodsOrderMapper.selectAll();
        return new PageResult<GoodsOrder>(orders.getTotal(),orders.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<GoodsOrder> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return goodsOrderMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<GoodsOrder> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<GoodsOrder> orders = (Page<GoodsOrder>) goodsOrderMapper.selectByExample(example);
        return new PageResult<GoodsOrder>(orders.getTotal(),orders.getResult());
    }

    /**
     * 根据Id查询
     * @param
     * @return
     */
    public GoodsOrder findById(String orderId) {
        return goodsOrderMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 新增
     * @param order
     */
    public void add(GoodsOrder order) {
        goodsOrderMapper.insert(order);
    }

    /**
     * 修改
     * @param order
     */
    public void update(GoodsOrder order) {
        goodsOrderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     *  删除
     * @param
     */
    public void delete(String orderId) {
        goodsOrderMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(GoodsOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 订单id
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andLike("orderId","%"+searchMap.get("orderId")+"%");
            }
            // 买家信息
            if(searchMap.get("buyerMessage")!=null && !"".equals(searchMap.get("buyerMessage"))){
                criteria.andLike("buyerMessage","%"+searchMap.get("buyerMessage")+"%");
            }
            // 装运代码
            if(searchMap.get("shippingCode")!=null && !"".equals(searchMap.get("shippingCode"))){
                criteria.andLike("shippingCode","%"+searchMap.get("shippingCode")+"%");
            }
            // 装运名称
            if(searchMap.get("shippingName")!=null && !"".equals(searchMap.get("shippingName"))){
                criteria.andLike("shippingName","%"+searchMap.get("shippingName")+"%");
            }

            // 买家价格
            if(searchMap.get("buyerRate")!=null ){
                criteria.andEqualTo("buyerRate",searchMap.get("buyerRate"));
            }
            // 发票类型
            if(searchMap.get("invoiceType")!=null ){
                criteria.andEqualTo("invoiceType",searchMap.get("invoiceType"));
            }
            // 付款类型
            if(searchMap.get("paymentType")!=null ){
                criteria.andEqualTo("paymentType",searchMap.get("paymentType"));
            }
            // source_type
            if(searchMap.get("sourceType")!=null ){
                criteria.andEqualTo("sourceType",searchMap.get("sourceType"));
            }

        }
        return example;
    }

}
